package com.zjeav.lingjiao.ui.personCenter;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.dalimao.corelibrary.swipeback.SwipeBackActivity;
import com.dalimao.corelibrary.view.LoadProgressDialog;
import com.dalimao.corelibrary.view.SelfDialog;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.UiError;
import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.baseBean.BaseApplication;
import com.zjeav.lingjiao.base.listener.CallBackTypeInteraction;
import com.zjeav.lingjiao.base.request.LoginRequest;
import com.zjeav.lingjiao.ui.login.LoginContract;
import com.zjeav.lingjiao.ui.login.UserPresenter;

public class ChangeMobileActivity extends SwipeBackActivity implements LoginContract.View, IUiListener, CallBackTypeInteraction {

    EditText username_ed;
    EditText pw_ed;
    Button login_bn;

    UserPresenter userPresenter;
    private LoadProgressDialog loadProgressDialog; //加载框

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.chanage_mobile);

        login_bn = findViewById(R.id.login_bn);
        username_ed = findViewById(R.id.username_ed);
        pw_ed = findViewById(R.id.pw_ed);

        userPresenter = new UserPresenter(this);
        UserPresenter.setBRInteractionListener(this);
        loadProgressDialog = new LoadProgressDialog(this, com.dalimao.corelibrary.R.style.MyDialog);

        login_bn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (username_ed.getText().toString().equals("")) {
                    Toast.makeText(ChangeMobileActivity.this, "请输入手机", Toast.LENGTH_SHORT).show();
                } else if (pw_ed.getText().toString().equals("")) {
                    Toast.makeText(ChangeMobileActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                } else {
                    loadProgressDialog.show();
                    LoginRequest request = new LoginRequest(username_ed.getText().toString(), pw_ed.getText().toString(), BaseApplication.deviceInfo);
                    userPresenter.login(request);
                }
            }
        });
    }

    @Override
    public void onComplete(Object o) {

    }

    @Override
    public void onError(UiError uiError) {

    }

    @Override
    public void onCancel() {

    }

    @Override
    public void showError(Throwable throwable) {

    }

    @Override
    public void skipActivity() {
        loadProgressDialog.dismiss();
        Intent intent = new Intent(this, VerifyMobileActivity.class);
        intent.putExtra("type","3");
        startActivityForResult(intent,5);
    }

    @Override
    public void dissDiagle() {

    }

    @Override
    public void callBackType(String type) {
        loadProgressDialog.dismiss();
        final SelfDialog selfDialog = new SelfDialog(this);
        selfDialog.setTitle("验证失败");
        selfDialog.setMessage("账号或密码错误");
        selfDialog.setYesOnclickListener("确定", new SelfDialog.onYesOnclickListener() {
            @Override
            public void onYesClick() {
                selfDialog.dismiss();
            }
        });
        selfDialog.setNoOnclickListener("取消", new SelfDialog.onNoOnclickListener() {
            @Override
            public void onNoClick() {
                selfDialog.dismiss();
            }
        });
        selfDialog.show();
    }

    @Override
    public void callBackType(String type, Intent intent) {

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==5&& resultCode==5){
            String phone = data.getExtras().getString("editname","");
            Intent intent = new Intent();
            intent.putExtra("editname",phone);
            setResult(5,intent);
            finish();
        }
    }
}
